<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="4" labelWidth="85" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="whmClainManagement"
      selectionType="single"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectData"
    >
      <!--列操作-->
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addEstimate">新增</el-button>
        <el-button size="small" type="primary" icon="el-icon-delete" @click="delEstimate">删除</el-button>
        <el-button size="small" type="primary" icon="el-icon-upload" @click="upload">上传</el-button>
        <el-button size="small" type="primary" @click="getInformation">延保查询</el-button>
        <el-button size="small" type="primary" @click="getState">查询延保估算处理状态</el-button>
        <!-- <el-button size="small" type="primary" icon="el-icon-plus" >打印延保估算单</el-button> -->
      </template>
    </FilterTable>

    <!--抽屉 -->
    <Drawer :visible.sync="estimate" :title="dialogTitle" :width="1100" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addEstimateModal :type="handleType" :rowData="formData" @close="closeEstimate" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { getSeries, getEstimateOddNumbers, delEstimate, getUpdateGSDSave } from '@/api/repair/estimateExtension';
import { getPinpSelect, getChexSelect, getCheModelSelect, getChexSelectTable, getCheModelSelectTable } from '@/api/repair/repairReception/customer/brand.js';
import { getAllUser } from '@/api/finance/repairFinance/closeBalance';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { getModel } from '@/api/common/cars/brandcarsModels';
import addEstimateModal from './addEstimateModal';
 import { exGetUrl} from '@/api/repair/repairReception/customer';
// 延保订单查询
export default {
  name: 'wjfIndex',
  components: {
    addEstimateModal
  },
  mixins: [dictionary],
  data() {
    return {
      // wjf 2020年1月2日
      fetchParams: { xhrAbort: false },
      dialogTitle: '',
      fetchApi: getEstimateOddNumbers,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      tableData: [],
      selectFlag: false,
      topFilterList: this.createTopFilterList(),
      estimate: false,
      estimatequery:'' // 延保查询地址
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    // wjf 2020年1月2日
    this.getService();
    this.personnel();
    this.exGetUrl();
  },
  methods: {
    // 获取各系统的地址
    async exGetUrl() {
      let res = await exGetUrl({estimate:'estimate' });
      if (res.data) {
        this.estimatequery=res.data.estimate;
      }
    },
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesCode',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '延保估算单号',
          fieldName: 'ybdd',
          placeholder: '请输入...'
        },
        {
          type: 'CHECKBOX',
          labelOptions: {
            type: 'CHECKBOX',
            fieldName: '9105',
            options: {
              trueValue: 10041001,
              falseValue: 10041002
            }
          },
          fieldName: '9105',
          render: () => {
            return <span>已进厂延保估算单</span>;
          }
        },
        {
          type: 'RANGE_DATE',
          label: '估算日期',
          fieldName: 'beginDateFrom|endDateFrom',
          placeholder: '请输入...'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          minWidth: '100',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.emitFun(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '延保估算单号',
          dataIndex: 'EXT_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '估算日期',
          dataIndex: 'EXT_DATE',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '维修状态',
          dataIndex: 'REPAIR_STATUS',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('9638'),
          render: props => {
            return <span>{this.createDictText(props.row.REPAIR_STATUS, '9638')}</span>;
          }
        },
        {
          title: '服务顾问',
          dataIndex: 'SV_USER',
          sorter: true,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input'
        },
        {
          title: '上传日期',
          dataIndex: 'UP_DATE',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '上传状态',
          dataIndex: 'UP_STATUS',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上传状态描述',
          dataIndex: 'UP_REMARKS',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '延保申请单号',
          dataIndex: 'UP_EXT_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '延保申请日期',
          dataIndex: 'UP_EXT_DATE',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '车系代码',
          dataIndex: 'SERIES_CODE',
          sorter: true,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input'
        },
        {
          title: '底盘号',
          dataIndex: 'VIN',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input'
        },
        {
          title: '发动机号',
          dataIndex: 'ENGINE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车牌号',
          dataIndex: 'PLATE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'CUS_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户移动电话',
          dataIndex: 'TELPHONE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '生产日期',
          dataIndex: 'PRODUCT_DATE',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '购车日期',
          dataIndex: 'SALES_DATE',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '车型代码',
          dataIndex: 'MODEL_CODE',
          sorter: true,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'MODEL_NAME',
          width: 200,
          sorter: true,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input'
        },
        {
          title: '行驶里程',
          dataIndex: 'MILAGE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '故障描述',
          dataIndex: 'FAULT_REMARKS',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作员',
          dataIndex: 'CREATED_BY',
          sorter: true,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'REMARKS',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '应收工时费',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '应收材料费',
          dataIndex: 'REPAIR_PART_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '优惠金额',
          dataIndex: 'DERATE_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '优惠后合计',
          dataIndex: 'sss',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 删除按钮
    delEstimate() {
      if (!this.selectFlag) {
        this.$message({
          type: 'warning',
          message: '请选择表单数据!'
        });
      } else {
        this.$confirm('是否确定删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.delRow();
          })
          .catch(() => {});
      }
    },
    async delRow() {
      let delData = this.tableData;
      let list = [];
      for (const item of delData) {
        if (item.UP_STATUS != '0') {
          this.$notify.error({ title: '提示信息', message: '已上传数据不允许删除' });
          return;
        } else {
          list.push(item.EXT_NO);
        }
      }
      let res = await delEstimate({ delextNo: list.toString() });
      if (res.resultCode === 200) {
        this.$notify.success({ title: '提示信息', message: res.errMsg || '删除成功!' });
        this.fetchParams = { ...this.fetchParams };
      } else {
        this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
      }
      this.fetchParams = { ...this.fetchParams };
    },
    emitFun(val) {
      if (val.UP_STATUS == 'S') {
        this.handleType = 'detailed';
        this.dialogTitle = `延保估算单明细`;
      } else if (val.UP_STATUS == '0') {
        this.handleType = 'edit';
        this.dialogTitle = `编辑延保估算单`;
      }
      this.formData = val;
      this.estimate = true;
    },
    async getService() {
      const res = await getChexSelectTable();
      console.log(res, 'kkkkkkkkkkkk');
      this.columns.find(x => x.dataIndex === 'SERIES_CODE').editItems = res.data.map(x => ({ value: x.seriesId + '', text: x.seriesName }));
      this.columns.find(x => x.dataIndex === 'SERIES_NAME').editItems = res.data.map(x => ({ value: x.seriesId + '', text: x.seriesName }));
      this.topFilterList.find(x => x.fieldName === 'seriesCode').itemList = res.data.map(x => ({ value: x.seriesId + '', text: x.seriesName }));
      const res1 = await getCheModelSelectTable();
      this.columns.find(x => x.dataIndex === 'MODEL_CODE').editItems = res1.data.map(x => ({ value: x.modelId + '', text: x.modelName, model: x.modelCode }));
      this.columns.find(x => x.dataIndex === 'MODEL_NAME').editItems = res1.data.map(x => ({ value: x.modelId + '', text: x.modelName, model: x.modelCode }));
      // console.log('kkkkkkkkk',this.createDictList('9638'));
      // this.columns.find(x => x.dataIndex === 'REPAIR_STATUS').editItems = this.createDictList('9638');
    },
    // 服务顾问下拉选
    async personnel() {
      let testDrive = { role: dictCode.SERVICE_CONSULTANT };
      let res5 = (await searchRoleByCode(testDrive)).data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      this.columns.find(item => item.dataIndex === 'SV_USER').editItems = res5;
      // 操作员
      this.columns.find(x => x.dataIndex === 'CREATED_BY').editItems = (await getAllUser()).data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },
    addEstimate() {
      // 新建
      this.handleType = 'add';
      this.showHandler();
    },
    showHandler() {
      // 显示
      this.dialogTitle = `新增延保估算单`;
      this.estimate = true;
    },
    selectData(val) {
      this.tableData = val;
      if (val.length == 0) {
        this.selectFlag = false;
      } else {
        this.selectFlag = true;
      }
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async upload() {
      if (this.tableData.length > 0) {
        if (this.tableData[0].UP_STATUS == '0') {
          let extNo = { EXT_NO: this.tableData[0].EXT_NO };
          let res = await getUpdateGSDSave(extNo);
          if (res.resultCode === 200) {
            this.$notify({
              title: '提示信息',
              message: '上传成功！',
              type: 'success'
            });
            this.fetchParams = { ...this.fetchParams };
          }
        } else {
          this.$notify({
            title: '提示信息',
            message: '选择的数据已上传！',
            type: 'warning'
          });
        }
      } else {
        this.$notify({
          title: '提示信息',
          message: '请选择一条记录！',
          type: 'warning'
        });
      }
    },
    getInformation() {
      if (this.tableData.length > 0) {
        let ownerCode = this.tableData[0].OWNER_CODE;
        let vin = this.tableData[0].VIN;
        let f = document.createElement('form'); // 表单
        f.style.display = 'none'; // 显示为none
        f.target = '_blank'; // 打开新页面
        f.action =
          'https://portal.faw-vw.com/ewocs/faces/adf.task-flow?adf.tfId=ewocs_vw_afs_withoutLogin&adf.tfDoc=/WEB-INF/taskflows/ewocs_vw/afs/ewocs_vw_afs_withoutLogin.xml&wxz=' +
          ownerCode +
          '&vin=' +
          vin; // 地址
        console.log(f.action, '地址');
        f.method = 'post';
        document.body.appendChild(f);
        f.submit();
      } else {
        this.$notify({
          title: '提示信息',
          message: '请选择一条记录！',
          type: 'warning'
        });
      }
    },
    getState() {
      if (this.tableData.length > 0) {
        let ownerCode = this.tableData[0].OWNER_CODE;
        let extNo = this.tableData[0].EXT_NO;
        let f = document.createElement('form'); // 表单
        f.style.display = 'none'; // 显示为none
        f.target = '_blank'; // 打开新页面
        f.action =
          'https://portal.faw-vw.com/ewocs/faces/adf.task-flow?adf.tfId=ewocs_gsd_status_withoutLogin&adf.tfDoc=/WEB-INF/taskflows/ewocs_vw/Get_gsd_Status/ewocs_gsd_status_withoutLogin.xml&wxz=' +
          ownerCode +
          '&gswtsh=' +
          extNo; // 地址
        f.method = 'get';
        document.body.appendChild(f);
        f.submit();
      } else {
        this.$notify({
          title: '提示信息',
          message: '请选择一条记录！',
          type: 'warning'
        });
      }
    },
    closeEstimate(val, type) {
      // 关闭
      this.estimate = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
